package com.lmx.common.util;

import java.math.BigDecimal;

/**
 * 数字、价格工具
 * 
 * @author Jason
 * 
 */
public class NumberTool {

	/**
	 * Bigdecimal计算除法，numberA/numberB。numberA和numberB会转string后转成bigdecimal
	 * 
	 * @param numberA
	 *            除数
	 * @param numberB
	 *            被除数
	 * @return
	 */
	public static BigDecimal divide(Object numberA, Object numberB) {
		return divide(numberA, numberB, 2, BigDecimal.ROUND_HALF_UP);
	}
	
	/**
	 * Bigdecimal计算除法，numberA/numberB。numberA和numberB会转string后转成bigdecimal
	 * 默认四舍五入
	 * 
	 * @param numberA
	 *            除数
	 * @param numberB
	 *            被除数
	 * @param scale
	 *            保留位数
	 * @return
	 */
	public static BigDecimal divide(Object numberA, Object numberB, int scale) {
		return divide(numberA, numberB, scale, BigDecimal.ROUND_HALF_UP);
	}
	
	/**
	 * Bigdecimal计算除法，numberA/numberB。numberA和numberB会转string后转成bigdecimal
	 * 
	 * @param numberA
	 *            除数
	 * @param numberB
	 *            被除数
	 * @param scale
	 *            保留位数
	 * @param decimalRound
	 *            进位保留方式
	 * @return
	 */
	public static BigDecimal divide(Object numberA, Object numberB, int scale, int decimalRound) {
		return new BigDecimal(numberA.toString()).divide(
				new BigDecimal(numberB.toString()), scale,
				decimalRound);
	}
	
	/**
	 * Bigdecimal加法，所有numbers相加
	 * 
	 * @param numbers
	 * @return
	 */
	public static BigDecimal add(Object... numbers) {
		BigDecimal rs = new BigDecimal("0.00");
		for (Object num : numbers) {
			rs = rs.add(new BigDecimal(num.toString()));
		}
		return rs;
	}

	/**
	 * Bigdecimal减法，第一个数减去后面的数
	 * 
	 * @param numberA
	 * @param numberB
	 * @return
	 */
	public static BigDecimal subtract(Object... numbers) {
		if (numbers.length == 0) {
			return new BigDecimal("0.00");
		}
		BigDecimal numberA = new BigDecimal(numbers[0].toString());
		for (int i = 1; i < numbers.length; i++) {
			numberA = numberA.subtract(new BigDecimal(numbers[i].toString()));
		}
		return numberA;
	}

	/**
	 * Bigdecimal乘法，所有numbers相乘
	 * 
	 * @param numbers
	 * @return
	 */
	public static BigDecimal multiply(Object... numbers) {
		if (numbers.length == 1)
			return new BigDecimal(numbers[0].toString());
		BigDecimal rs = new BigDecimal(1);
		for (Object num : numbers) {
			rs = rs.multiply(new BigDecimal(num.toString()));
		}
		return rs;
	}
	
	/**
	 * Bigdecimal乘法，所有numbers相乘
	 * 
	 * @param numberA	乘数1
	 * @param numberB	乘数2
	 * @param scalee	保留位数
	 * @param bigdecimalRound	进位保留类型BigDecimal.Round_...
	 * @return
	 */
	public static BigDecimal multiply(Object numberA, Object numberB, int scale, int bigdecimalRound) {
		return new BigDecimal(numberA.toString())
				.multiply(new BigDecimal(numberB.toString()))
				.setScale(scale, bigdecimalRound);
	}

	/**
	 * 格式化数字，保留两位小数
	 * 
	 * @param number
	 * @return
	 */
	public static BigDecimal format2Scale(Object number) {
		if (number.equals(""))
			return new BigDecimal("0.00");
		return new BigDecimal(number.toString()).setScale(2,
				BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * 格式化数字，保留三位小数
	 * 
	 * @param number
	 * @return
	 */
	public static BigDecimal format3Scale(Object number) {
		if (number.equals(""))
			return new BigDecimal("0.000");
		return new BigDecimal(number.toString()).setScale(3,
				BigDecimal.ROUND_HALF_UP);
	}

	/**
	 * 格式化价格，保留两位小数
	 * 
	 * @param price
	 * @return
	 */
	public static BigDecimal formatPrice(Object price) {
		if (price.equals(""))
			return new BigDecimal("0.00");
		return new BigDecimal(price.toString()).setScale(2,
				BigDecimal.ROUND_HALF_UP);
	}

	public static String formatPriceStr(String price) {
		if (price.equals(""))
			return "0.00";
		return format2Scale(price).toString();
	}
	
	/**
	 * 获取String类型的整数，""返回"0"，小数部分忽略
	 * @param integer
	 * @return
	 */
	public static String getIntegerStr(String integer) {
		if (integer.equals(""))
			return "0";
		return Double.valueOf(integer).intValue() + "";
	}
	
	/**
	 * 获取整数，""返回0，小数部分忽略
	 * @param integer
	 * @return
	 */
	public static Integer getInteger(Object integer) {
		if (integer.equals(""))
			return 0;
		return Double.valueOf(integer.toString()).intValue();
	}

	/**
	 * 将‘分’转为‘元’
	 * 
	 * @param integer
	 * @return
	 */
	public static BigDecimal parseCentToYuan(Object cent) {
		return divide(cent, 100);
	}
	
	/**
	 * 忽略null和""为0，返回bigdecimal类型
	 * 
	 * @param num
	 * @return
	 */
	public static BigDecimal getNumberNullTo0(Object num) {
		return new BigDecimal((num == null || num.equals("") ? "0" : num.toString()));
	}

	/**
	 * 将‘元’转为‘分’
	 * 
	 * @param integer
	 * @return
	 */
	public static BigDecimal parseYuanToCent(Object yuan) {
		return multiply(yuan, 100);
	}

	public static void main(String[] args) {
		System.out.println(subtract(96666,1));
	}
}
